/**
 * Copyright (c) 2011-2014, hubin (jobob@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.baomidou.mybatisplus.shardingjdbc.spring.boot;

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

/**
 * <p>
 * Mybatis全局缓存
 * </p>
 *
 * @author Caratacus
 * @since 2017-05-01
 */
public class GlobalConfiguration {

    /**
     * 主键值生成器类型名称，具体可参考{@link IdType}
     */
    private String idType;
    /**
     * 表名、字段名、是否使用下划线命名
     */
    private Boolean dbColumnUnderline;
    /**
     * SQL注入器
     */
    @Deprecated
    private String sqlInjector;
    /**
     * 元对象字段填充控制器
     */
    @Deprecated
    private String metaObjectHandler;
    /**
     * 字段验证策略
     *
     * @deprecated 3.1.2 please use {@link #insertStrategy} and {@link #updateStrategy} and {@link #selectStrategy}
     */
    @Deprecated
    private String fieldStrategy = "NOT_NULL";

    /**
     * 字段验证策略之 insert
     * 目前没有默认值,等 {@link #fieldStrategy} 完全去除掉,会给个默认值 NOT_NULL。参数值参考{@link FieldStrategy}
     * 没配则按 {@link #fieldStrategy} 为准
     *
     * @since 3.1.2
     */
    private String insertStrategy;
    /**
     * 字段验证策略之 update
     * 目前没有默认值,等 {@link #fieldStrategy} 完全去除掉,会给个默认值 NOT_NULL。参数值参考{@link FieldStrategy}
     * 没配则按 {@link #fieldStrategy} 为准
     *
     * @since 3.1.2
     */
    private String updateStrategy;
    /**
     * 字段验证策略之 select
     * 目前没有默认值,等 {@link #fieldStrategy} 完全去除掉,会给个默认值 NOT_NULL。参数值参考{@link FieldStrategy}
     * 没配则按 {@link #fieldStrategy} 为准
     *
     * @since 3.1.2
     */
    private String selectStrategy;

    /**
     * 方便调试
     */
    private Boolean refreshMapper;

    /**
     * 是否大写命名
     */
    private Boolean isCapitalMode;
    /**
     * 标识符
     */
    private String identifierQuote;
    /**
     * 逻辑删除全局值,（默认 1、表示已删除）
     */
    private String logicDeleteValue = "1";
    /**
     * 逻辑未删除全局值,（默认 0、表示未删除）
     */
    private String logicNotDeleteValue = "0";
    /**
     * 表关键词 key 生成器
     */
    @Deprecated
    private String keyGenerator;
    /**
     * 缓存 Sql 解析初始化
     */
    private Boolean sqlParserCache;

    /**
     * 表名前缀
     */
    private String tablePrefix;

    public String getIdType() {
        return idType;
    }

    public void setIdType(String idType) {
        this.idType = idType;
    }

    public Boolean getDbColumnUnderline() {
        return dbColumnUnderline;
    }

    public void setDbColumnUnderline(Boolean dbColumnUnderline) {
        this.dbColumnUnderline = dbColumnUnderline;
    }

    public String getSqlInjector() {
        return sqlInjector;
    }

    public void setSqlInjector(String sqlInjector) {
        this.sqlInjector = sqlInjector;
    }

    public String getMetaObjectHandler() {
        return metaObjectHandler;
    }

    public void setMetaObjectHandler(String metaObjectHandler) {
        this.metaObjectHandler = metaObjectHandler;
    }

    public String getFieldStrategy() {
        return fieldStrategy;
    }

    public void setFieldStrategy(String fieldStrategy) {
        this.fieldStrategy = fieldStrategy;
    }

    public Boolean getCapitalMode() {
        return isCapitalMode;
    }

    public void setCapitalMode(Boolean capitalMode) {
        isCapitalMode = capitalMode;
    }

    public String getIdentifierQuote() {
        return identifierQuote;
    }

    public void setIdentifierQuote(String identifierQuote) {
        this.identifierQuote = identifierQuote;
    }

    public Boolean getRefreshMapper() {
        return refreshMapper;
    }

    public void setRefreshMapper(Boolean refreshMapper) {
        this.refreshMapper = refreshMapper;
    }

    public String getLogicDeleteValue() {
        return logicDeleteValue;
    }

    public void setLogicDeleteValue(String logicDeleteValue) {
        this.logicDeleteValue = logicDeleteValue;
    }

    public String getLogicNotDeleteValue() {
        return logicNotDeleteValue;
    }

    public void setLogicNotDeleteValue(String logicNotDeleteValue) {
        this.logicNotDeleteValue = logicNotDeleteValue;
    }

    public String getKeyGenerator() {
        return keyGenerator;
    }

    public void setKeyGenerator(String keyGenerator) {
        this.keyGenerator = keyGenerator;
    }

    public Boolean getSqlParserCache() {
        return sqlParserCache;
    }

    public void setSqlParserCache(Boolean sqlParserCache) {
        this.sqlParserCache = sqlParserCache;
    }

    public String getInsertStrategy() {
        return insertStrategy;
    }

    public void setInsertStrategy(String insertStrategy) {
        this.insertStrategy = insertStrategy;
    }

    public String getUpdateStrategy() {
        return updateStrategy;
    }

    public void setUpdateStrategy(String updateStrategy) {
        this.updateStrategy = updateStrategy;
    }

    public String getSelectStrategy() {
        return selectStrategy;
    }

    public void setSelectStrategy(String selectStrategy) {
        this.selectStrategy = selectStrategy;
    }


    public String getTablePrefix() {
        return tablePrefix;
    }

    public void setTablePrefix(String tablePrefix) {
        this.tablePrefix = tablePrefix;
    }

    public GlobalConfig convertGlobalConfig() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        GlobalConfig globalConfiguration = new GlobalConfig();
        GlobalConfig.DbConfig dbConfig = globalConfiguration.getDbConfig();
        //if (StringUtils.isNotEmpty(this.getIdentifierQuote())) {
        //    dbConfig.setIdentifierQuote(this.getIdentifierQuote());
        //}
        if (StringUtils.isNotEmpty(this.getLogicDeleteValue())) {
            dbConfig.setLogicDeleteValue(this.getLogicDeleteValue());
        }
        if (StringUtils.isNotEmpty(this.getLogicNotDeleteValue())) {
            dbConfig.setLogicNotDeleteValue(this.getLogicNotDeleteValue());
        }
        if (StringUtils.isNotEmpty(this.getSqlInjector())) {
            globalConfiguration.setSqlInjector((ISqlInjector) Class.forName(this.getSqlInjector()).newInstance());
        }
        if (StringUtils.isNotEmpty(this.getMetaObjectHandler())) {
            globalConfiguration.setMetaObjectHandler((MetaObjectHandler) Class.forName(this.getMetaObjectHandler()).newInstance());
        }
        if (StringUtils.isNotEmpty(this.getKeyGenerator())) {
            dbConfig.setKeyGenerator((IKeyGenerator) Class.forName(this.getKeyGenerator()).newInstance());
        }
        if (StringUtils.isNotEmpty(this.getIdType())) {
            dbConfig.setIdType(IdType.valueOf(this.getIdType().toUpperCase()));
        }
        if (null != this.getDbColumnUnderline()) {
            dbConfig.setColumnLike(this.getDbColumnUnderline());
        }
        if (StringUtils.isNotEmpty(this.getTablePrefix())) {
            dbConfig.setTablePrefix(this.getTablePrefix());
        }
        if (StringUtils.isNotEmpty(this.getFieldStrategy())) {
            dbConfig.setFieldStrategy(FieldStrategy.valueOf(this.getFieldStrategy().toUpperCase()));
        }
        if (StringUtils.isNotEmpty(this.getInsertStrategy())) {
            dbConfig.setInsertStrategy(FieldStrategy.valueOf(this.getInsertStrategy().toUpperCase()));
        }
        if (StringUtils.isNotEmpty(this.getUpdateStrategy())) {
            dbConfig.setUpdateStrategy(FieldStrategy.valueOf(this.getUpdateStrategy().toUpperCase()));
        }
        if (StringUtils.isNotEmpty(this.getSelectStrategy())) {
            dbConfig.setInsertStrategy(FieldStrategy.valueOf(this.getInsertStrategy().toUpperCase()));
        }
        // if (StringUtils.checkValNotNull(this.getRefreshMapper())) {
        //     globalConfiguration.setRefreshMapper(this.getRefreshMapper());
        // }
        if (StringUtils.checkValNotNull(this.getCapitalMode())) {
            dbConfig.setCapitalMode(this.getCapitalMode());
        }
        if (null != this.getSqlParserCache()) {
            globalConfiguration.setSqlParserCache(this.getSqlParserCache());
        }
        return globalConfiguration;
    }

}
